package ldh.im.fxgui.controller.handler;

import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.ListView;
import ldh.im.fxbase.cell.ObjectListCell;
import ldh.im.fxbase.pojo.DbMessage;

import java.util.List;

public class SendMessageListViewHandler implements SendMessageHandler {

    private ListView<DbMessage> messageList;

    @Override
    public Node createMessagePaneNode() {
        messageList = new ListView<>();
        messageList.setCellFactory(new ObjectListCell<>(message -> createMessagePane(message)));
//        messageList.setCellCallback(message -> createMessagePane((Message)message));
        return messageList;
    }

    @Override
    public void addMessage(DbMessage message) {
        messageList.getItems().add(message);
        messageList.scrollTo(message);
    }

    @Override
    public void addMessages(List<DbMessage> messages) {
        messageList.getItems().addAll(messages);
        messageList.scrollTo(messageList.getItems().size());
    }

    @Override
    public ObservableList<DbMessage> getItems() {
        return messageList.getItems();
    }
}
